/*******************************************************************************
 * Copyright (C) 2010 Devin Samarin.
 * 
 * This file is part of the SUIT Toolkit.
 * 
 * The SUIT Toolkit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * The SUIT Toolkit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the SUIT Toolkit.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package suit.ui;

/**
 * Contains miscellaneous functions for anything
 * 
 * @author Devin Samarin
 * 
 */
public class Misc {

	/**
	 * Returns the number of times a certain character is found within a string
	 * 
	 * @param str
	 *            the string to search in
	 * @param ch
	 *            the character to look for
	 * @return the number of times ch is found in str
	 */
	public static int count(String str, char ch) {
		int count = 0;
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == ch) {
				count++;
			}
		}
		return count;
	}

	/**
	 * Split the string around the matches of a character. Trailing empty
	 * strings are included in the resulting array.
	 * 
	 * @param str
	 *            the String to split
	 * @param deliminator
	 *            the character to look for and split around
	 * @return the array of strings computed by splitting this string around
	 *         matches of the deliminator
	 */
	public static String[] splitString(String str, char deliminator) {
		int start = 0;
		int end = 0;
		int count = count(str, deliminator);
		String[] arr = new String[count + 1];
		count = 0;
		while ((end = str.indexOf(deliminator, start)) >= 0) {
			arr[count++] = str.substring(start, end);
			start = end + 1;
		}
		if (start < str.length()) {
			arr[count] = str.substring(start, str.length());
		}
		return arr;
	}

	/**
	 * Out of an array of strings, return the element which contains the most
	 * characters
	 * 
	 * @param str
	 *            a string array
	 * @return a String containing the longest line of the str array
	 */
	public static String longestLine(String[] str) {
		int len = str.length;
		int l = 0;
		String longest = "";
		for (int i = 0; i < len; i++) {
			if (str[i].length() > l) {
				longest = str[i];
				l = longest.length();
			}
		}
		return longest;
	}

	/**
	 * Given an array of characters, return true if the specified character is
	 * contained in the array.
	 * 
	 * @param array
	 *            an array of characters
	 * @param ch
	 *            the character to look for
	 * @return true is ch is found in array
	 */
	public static boolean inArray(char[] array, char ch) {
		int len = array.length;
		boolean is = false;
		for (int i = 0; i < len; i++) {
			if (array[i] == ch) {
				is = true;
				break;
			}
		}
		return is;
	}
}
